/*
  倒计时模块
  val  目标时间点
  timetype
    默认为1  返回 天时分秒
         2   返回 时分秒
         3   返回 xx:xx:xx
*/

let gettime = (val, timetype = 1) => {
    let nowtime = new Date().getTime() / 1000;
    let t = parseInt(val) - parseInt(nowtime)
    if (t > 0) {
        let d = totwo(Math.floor(t / 60 / 60 / 24));
        let h = totwo(Math.floor(t / 60 / 60 % 24));
        let m = totwo(Math.floor(t / 60 % 60));
        let s = totwo(Math.floor(t % 60));
        if (timetype == 1) {
            return `${d}天${h}时${m}分${s}秒`
        } else if (timetype == 2) {
            h = totwo(parseInt(h) + d * 24)
            return {
                h: h,
                m: m,
                s: s,
                tips: `${h}时${m}分${s}秒`
            }
        } else if (timetype == 3) {
            h = totwo(parseInt(h) + d * 24)
            return {
                h: h,
                m: m,
                s: s,
                tips: `${h}:${m}:${s}`
            }
        }
    } else {
        return false
    }
}

const totwo = val => {
    val = val.toString();
    return val[1] ? val : '0' + val
}

module.exports = {
    gettime
}